package com.hou.shopping.product.service.impl;

import com.hou.shopping.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hou.shopping.common.utils.PageUtils;
import com.hou.shopping.common.utils.Query;

import com.hou.shopping.product.dao.CategoryDao;
import com.hou.shopping.product.entity.CategoryEntity;
import com.hou.shopping.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

//    @Autowired
//    CategoryDao categoryDao;

    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    //    1.查出所有分类、组装为父子树形结构
    @Override
    public List<CategoryEntity> listWithTree() {
//        1.查询所有的分类
        List<CategoryEntity> categoryentities = baseMapper.selectList(null);

//        2.组装为父子的树型结构
        List<CategoryEntity> level1Menus = categoryentities.stream().filter(categoryEntity ->
                        categoryEntity.getParentCid() == 0)
                .map(menu -> {
                    List<CategoryEntity> childrens = getChildrens(menu, categoryentities);
                    menu.setChildren(childrens);
                    return menu;
                }).sorted((menu1, menu2) -> {
                    return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
                }).collect(Collectors.toList());

        return level1Menus;
    }

    //    递归方式获取父子菜单
    private List<CategoryEntity> getChildrens(CategoryEntity root, List<CategoryEntity> all) {
        List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid() == root.getCatId();
        }).map(categoryEntity -> {
            categoryEntity.setChildren(getChildrens(categoryEntity, all));
            return categoryEntity;
        }).sorted((menu1, menu2) -> {
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());

        return children;
    }


    //    删除方法
    @Override
    public void removeMenuByIds(List<Long> asList) {
//        TODO:1.需要检查是否被引用
        baseMapper.deleteBatchIds(asList);
    }

    //    属性分组-级联选择器分类回显  路径
    @Override
    public Long[] findCatelogPaths(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        List<Long> parentPath = findParentPath(catelogId, paths);
        Collections.reverse(parentPath);
        return parentPath.toArray(new Long[parentPath.size()]);
    }

    //    级联更新所有的关联数据
    @Override
    public void updateDetail(CategoryEntity category) {
        this.updateById(category);
//        调用级联更新
        categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());
    }

    //    递归方法
    private List<Long> findParentPath(Long catelogId, List<Long> paths) {
//        1.收集当前节点的id
        paths.add(catelogId);
        CategoryEntity byId = this.getById(catelogId);//当前分类的id
        if (byId.getParentCid() != 0) {
            findParentPath(byId.getParentCid(), paths);
        }
        return paths;
    }
}
